﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using TerrainGenerator.Geometry;

namespace TerrainGenerator
{
    class Aim
    {
        float yaw;
        float pitch;

        KeyboardState prevKeyboardState = Keyboard.GetState();

        const float rotationSpeed = 0.01f;

        GraphicsDevice graphicsDevice;

        CylinderPrimitive cylinder;

        public Vector3 position;

        public Aim(GraphicsDevice graphicsDevice)
        {
            this.graphicsDevice = graphicsDevice;
            cylinder = new CylinderPrimitive(graphicsDevice);
            position = new Vector3(130, 0, 370);// Vector3.Zero;
        }

        public void Update(KeyboardState keyboardState)
        {
            if (keyboardState.IsKeyDown(Keys.Left))
            {
                yaw += rotationSpeed;
            }
            if (keyboardState.IsKeyDown(Keys.Right))
            {
                yaw -= rotationSpeed;
            }
            if (keyboardState.IsKeyDown(Keys.Up))
            {
                pitch += rotationSpeed;
            }
            if (keyboardState.IsKeyDown(Keys.Down))
            {
                pitch -= rotationSpeed;
            }

            prevKeyboardState = keyboardState;
        }

        public Vector3 GetForce(float power)
        {
            Matrix rotation = Matrix.CreateRotationX(pitch) * Matrix.CreateRotationY(yaw);
            Vector3 v = Vector3.Transform(Vector3.UnitZ, rotation);
            v.Normalize();
            return v * power;
        }

        public void Draw(Matrix world, Matrix view, Matrix projection)
        {
            BasicEffect effect = new BasicEffect(graphicsDevice, null);

            effect.World = world /* * Matrix.CreateRotationX(pitch) * Matrix.CreateRotationY(yaw)*/ * Matrix.CreateScale(100.0f) * Matrix.CreateTranslation(position);
            effect.View = view;
            effect.Projection = projection;

            effect.EnableDefaultLighting();

            // teken een pijl in de juiste richting...
            cylinder.Draw(effect);
        }
    }
}
